/*************************************************
Description:智能家居--卧室界面的实现，包括卧室床头的灯
**************************************************/
#include "bedroomwidget.h"
#include "ui_BedRoomWidget.h"
#include <QToolButton>
#include <QJsonParseError>
#include <QJsonValue>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QDebug>
#include <QSound>
#include "myapp.h"
#include "control.h"

BedRoomWidget::BedRoomWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BedRoomWidget)
{
    ui->setupUi(this);


    this->initForm();
    this->init();
    this->initConnect();
}

BedRoomWidget::~BedRoomWidget()
{
    delete ui;
}

void BedRoomWidget::initForm()
{
    ui->label_device->setVisible(false);
    //灯泡样式
    m_styleOn = "QLabel{border-image:url(:/images/module/Tablelamp_on.png);border:0px;}";
    m_styleOff = "QLabel{border-image:url(:/images/module/Tablelamp_off.png);border:0px;}";

    ui->pbn_light_right->setFocusPolicy(Qt::NoFocus);
    ui->pbn_light_left->setFocusPolicy(Qt::NoFocus);

    //根据配置文件初始化LED的状态
    if (Myapp::bed_room_led_left == true)
    {
        ui->label_light_top->setStyleSheet(m_styleOn);
        ui->pbn_light_left->SetCheck(true);
    }
    else
    {
        ui->label_light_top->setStyleSheet(m_styleOff);
        ui->pbn_light_left->SetCheck(false);
    }
    if (Myapp::bed_room_led_right == true)
    {
        ui->pbn_light_right->SetCheck(true);
        ui->label_light_help->setStyleSheet(m_styleOn);
    }
    else
    {
        ui->pbn_light_right->SetCheck(false);
        ui->label_light_help->setStyleSheet(m_styleOff);
    }
}

void BedRoomWidget::init()
{
}

//信号和槽初始化
void BedRoomWidget::initConnect()
{
    connect(ui->pbn_light_left,SIGNAL(clicked()),
            this,SLOT(slotLightLeft()));
    connect(ui->pbn_light_right,SIGNAL(clicked()),
            this,SLOT(slotLightRight()));
}

void BedRoomWidget::setLightStyle(const QString styleName)
{
    if (styleName == "m_styleOn")
    {
        ui->label_light_top->setStyleSheet(m_styleOn);
        ui->label_light_help->setStyleSheet(m_styleOn);
        ui->pbn_light_left->SetCheck(true);
        ui->pbn_light_right->SetCheck(true);
    }else
    {
        ui->label_light_top->setStyleSheet(m_styleOff);
        ui->label_light_help->setStyleSheet(m_styleOff);
        ui->pbn_light_left->SetCheck(false);
        ui->pbn_light_right->SetCheck(false);
    }
}

void BedRoomWidget::setLedTurnStatus(quint8 status)
{
    if (LED_ON == status)
    {
        ui->label_light_top->setStyleSheet(m_styleOn);
        ui->label_light_help->setStyleSheet(m_styleOn);
        ui->pbn_light_left->SetCheck(true);
        ui->pbn_light_right->SetCheck(true);
    }else if (LED_OFF == status)
    {
        ui->label_light_top->setStyleSheet(m_styleOff);
        ui->label_light_help->setStyleSheet(m_styleOff);
        ui->pbn_light_left->SetCheck(false);
        ui->pbn_light_right->SetCheck(false);
    }

}

//卧室左床头灯状态改变以及发送状态信号
void BedRoomWidget::slotLightLeft()
{
    if (ui->pbn_light_left->GetCheck())
    {
        ui->label_light_top->setStyleSheet(m_styleOn);
        QSound::play(":/sounds/switch.wav");
        emit signalLightLeft(910);
    }
    else
    {
        ui->label_light_top->setStyleSheet(m_styleOff);
        QSound::play(":/sounds/switch.wav");
        emit signalLightLeft(900);
    }

    //gIsSendMessage = true;
}

//卧室右床头灯状态改变以及发送状态信号
void BedRoomWidget::slotLightRight()
{
    if (ui->pbn_light_right->GetCheck())
    {
        ui->label_light_help->setStyleSheet(m_styleOn);
        QSound::play(":/sounds/switch.wav");
       emit signalLightRight(810);
    }
    else
    {
        ui->label_light_help->setStyleSheet(m_styleOff);
        QSound::play(":/sounds/switch.wav");
       emit signalLightRight(800);
    }
    //gIsSendMessage = true;
}

//更新显示温度和湿度值
void BedRoomWidget::slotUpdate(int key,QString val)
{
    if(key==301)
    {
    ui->label_cur_temp->setText(val);
    qDebug()<<"beedroom sensor:"<<val;
    }
    if(key==302)
    {
    ui->label_cur_hum->setText(val);
    qDebug()<<"beedroom bat:"<<val;
    }
}

//更新LED灯的状态
//void BedRoomWidget::slotUpdataLedStatus(quint8 device, quint8 cmd)
//{

//}

//保存灯的状态，写入配置文件
void BedRoomWidget::slotSaveLedStatus()
{
    Myapp::bed_room_led_left = ui->pbn_light_left->GetCheck();
    Myapp::bed_room_led_right = ui->pbn_light_right->GetCheck();
    Myapp::WriteConfig();
}

//用来改变LED的状态
//void BedRoomWidget::slotChangeLedStatus(quint8 status)
//{

//}
